package base.utils

import android.annotation.SuppressLint
import android.widget.Toast
import android.view.Gravity
import android.os.Looper
import base.sys.ApplicationContext
import base.sys.ApplicationContext.mContext
import android.widget.TextView
import android.widget.LinearLayout




/**
 * Created by Jack on 2017/9/26.
 */
object CustomToast {
    private var mToast: Toast? = null
    private var t: String? = null
    @SuppressLint("ShowToast")
    fun showToast(text: CharSequence) {
        if (t == null || t != text as String) {
            t = text as String
            mToast = try {
                Toast.makeText(ApplicationContext.application, text, Toast.LENGTH_SHORT)
            } catch (e: RuntimeException) {
                Looper.prepare()
                Toast.makeText(ApplicationContext.application, text, Toast.LENGTH_SHORT)
            }
            mToast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT)
            val layout = mToast!!.view as LinearLayout
            val tv = layout.getChildAt(0) as TextView
            tv.textSize = 25f
        } else {
            mToast!!.setText(text)
        }
                if (mToast != null){
                    mToast!!.setText(text);
                } else {
                    mToast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT)
                }
        mToast!!.setGravity(Gravity.CENTER, 0, 0)
        mToast!!.show()
    }

    fun showLongToast( text: CharSequence) {
        if (t == null || t != text as String) {
            t = text as String
            mToast = try {
                Toast.makeText(ApplicationContext.application, text, Toast.LENGTH_LONG)
            } catch (e: RuntimeException) {
                Looper.prepare()
                Toast.makeText(ApplicationContext.application, text, Toast.LENGTH_LONG)
            }
            mToast = Toast.makeText(mContext, text, Toast.LENGTH_LONG)
            val layout = mToast!!.view as LinearLayout
            val tv = layout.getChildAt(0) as TextView
            tv.textSize = 25f
        } else {
            mToast!!.setText(text)
        }
        if (mToast != null){
            mToast!!.setText(text);
        } else {
            mToast = Toast.makeText(mContext, text, Toast.LENGTH_LONG)
        }
        mToast!!.setGravity(Gravity.CENTER, 0, 0)
        mToast!!.show()
    }


}